package com.xhframework.core;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
 
public class ConfigUtil {
 
    @SuppressWarnings("unchecked")
    public static void parseConfigFile(String fileName,Map<String, ActionConfig> map) {
    	// 需要引入 jaxen-full.jar  和  saxpath。jar     	
       SAXReader reader = new SAXReader() ;
       try {
           Document doc = reader.read(new File(fileName));
           Element root = doc.getRootElement();
           List<Element> list = root.selectNodes("package/action");
           System.out.println(list.size());
           for(Element actionNode : list)
           {
              //封装成ActionConfig对象，保存到map中。
              ActionConfig config = new ActionConfig();
              //将action节点的path, type, name属性值获取到。
              String name = actionNode.attributeValue("name");
              String clzName = actionNode.attributeValue("class");
              String method = actionNode.attributeValue("method");
              config.setName(name);
              config.setClzName(clzName);
              if(method == null || "".equals(method)){
                  method="execute";
              }
              config.setMethod(method);
             
              //遍历action节点下的forward子节点
              Iterator itrNodes = actionNode.selectNodes("result").iterator();
              while(itrNodes.hasNext()){
                  Element forwardNode = (Element)itrNodes.next();
                  String forwardName = forwardNode.attributeValue("name");
                  String forwardPath = forwardNode.getTextTrim(); 
                 
                  if(forwardName == null || "".equals(forwardName)){
                     forwardName="success";
                  }
                  config.getResultMap().put(forwardName,forwardPath);
              }  
              map.put(name,config);
           }
       } catch (DocumentException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
    }
    
    //解析xml文档
    public static void Init(){
         		//只需要dom4j.jar 就可以
        try {
              //InputStream is=this.getClass().getResourceAsStream("/"+path);
              //解析xml
              Document doc=new SAXReader().read("C:\\struts.xml");
              //获取根节点
              Element root = doc.getRootElement();
              //获取actions节点
              Element actions=(Element)root.elementIterator("package").next();
              //使用for循环来遍历actions节点下的所有action节点
              for(Iterator<Element> action=actions.elementIterator("action");action.hasNext();)
              {
                  //获取到<action>节点
                  Element actionnext = action.next();
                  //分别获取到action节点中的name属性和class属性
                  String name = actionnext.attributeValue("name");
                  String classname = actionnext.attributeValue("class");
                  //将以上两个属性保存到ActionMapping类中
                 // ActionMapping mapp=new ActionMapping();
                 // mapp.setClassname(classname);
                 // mapp.setName(name);
                  //由于一个action节点下有多个result节点 遍历action下所有的result节点
                  for(Iterator<Element> result=actionnext.elementIterator("result");result.hasNext();)
                  {
                      //获取到result节点
                      Element resultnext = result.next();
                      //提取result节点的name属性值和result节点中的值
                      String resultname = resultnext.attributeValue("name");
                      String resultvalue=resultnext.getText();
                      //将其分别存入到actionMapping中的双列集合中去，方便调用actionMapping类(actionMapping类中就有数据了!)
                     // mapp.addResult(resultname, resultvalue);
                      System.out.println("resultname:"+resultname+";resultvalue:"+resultvalue);
                  }
                  //得到所有action节点的集合
                //maps.put(mapp.getName(), mapp);
                  System.out.println("name:"+name+";classname:"+classname);
              }
               
        } catch (Exception e) {
            // TODO: handle exception
        }
      }
 
}
